package com.xmx.promotioncenter.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
import com.xmx.promotioncenter.entity.GrouponRulesEntity;
import com.xmx.promotioncenter.service.GrouponRulesService;
import com.xmx.common.utils.PageUtils;
import com.xmx.common.utils.R;
import io.swagger.annotations.Api;


/**
 * 团购规则
 *
 * @author fbb
 * @email boyinbing@126.com
 * @date 2021-08-11 01:47:43
 */
@Api(tags = "GrouponRulesController", description = "团购规则服务")
@RestController
@RequestMapping("promotioncenter/grouponrules")
public class GrouponRulesController {
    @Autowired
    private GrouponRulesService grouponRulesService;

    /**
     * 列表
     */
    @GetMapping("/list1")
    //@RequiresPermissions("promotioncenter:grouponrules:list")
    @ApiOperation("获取所有团购规则信息")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = grouponRulesService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/list")
    //@RequiresPermissions("promotioncenter:grouponrules:list")
    @ApiOperation("获取所有团购规则信息")
    public R list1(){
        List<Map> maps = grouponRulesService.getgouponList();
        PageUtils page = new PageUtils(maps,maps.size(),1,1);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("promotioncenter:grouponrules:info")
    @ApiOperation("获取指定id的团购规则详情")
    public R info(@PathVariable("id") Integer id){
		GrouponRulesEntity grouponRules = grouponRulesService.getById(id);

        return R.ok().put("grouponRules", grouponRules);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加团购规则信息")
    //@RequiresPermissions("promotioncenter:grouponrules:save")
    public R save(@RequestBody GrouponRulesEntity grouponRules){
		grouponRulesService.save(grouponRules);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("更新指定id团购规则信息")
    //@RequiresPermissions("promotioncenter:grouponrules:update")
    public R update(@RequestBody GrouponRulesEntity grouponRules){
		grouponRulesService.updateById(grouponRules);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除指定id团购规则信息")
    //@RequiresPermissions("promotioncenter:grouponrules:delete")
    public R delete(@RequestBody Integer[] ids){
		grouponRulesService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/infos/{goods_id}")
    public R getBygoodsid(@PathVariable("goods_id") Integer id){
        QueryWrapper<GrouponRulesEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",id);
        List<GrouponRulesEntity> list = grouponRulesService.list(wrapper);
        PageUtils page = new PageUtils(list,list.size(),1,1);

        return R.ok().put("page", page);
    }

}
